package com.junxian.demo02.service.user.impl;

import com.github.pagehelper.PageHelper;
import com.junxian.demo02.dao.UserMapper;
import com.junxian.demo02.pojo.User;
import com.junxian.demo02.pojo.UserExample;
import com.junxian.demo02.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: SunJunxian
 * @Date: 2018/12/11
 * @Description: 实现User的接口的具体的方法。
 */

@Service(value = "userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        return userMapper.insertSelective(user);
    }

    /**
     * 查找全部用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<User> findAllUser(int pageNum , int pageSize) {
        PageHelper.startPage(pageNum , pageSize);
        return userMapper.selectAllUser();
    }

    @Override
    public int deleteUser(String name) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andNameEqualTo(name);
        return userMapper.deleteByExample(userExample);
    }

    @Override
    public int updateUser(String name , int age) {
        UserExample userExample = new UserExample();
        User user = new User();
        userExample.createCriteria().andNameEqualTo(name);
        user.setAge(age);
        return userMapper.updateByExampleSelective(user , userExample);
    }


}
